//
//  LHUIButton.m
//  UIButton的图片文字扩展
//
//  Created by LH on 17/3/20.
//  Copyright © 2017年 LH. All rights reserved.
//

#import "LHUIButton.h"
#define SPACING 10
@implementation LHUIButton


-(void)lh_setImage:(NSString *)imageStr butTitle:(NSString *)title types:(Type)types{
    
//    self.layer.borderWidth=0.5;
//    self.layer.borderColor=[UIColor lightGrayColor].CGColor;
    [self setImage:[UIImage imageNamed:imageStr] forState:UIControlStateNormal];
    [self setTitle:title forState:UIControlStateNormal];
    [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.type=types;


}
-(void)setType:(Type)type{
    //间距
    _spacing=SPACING/2;
    
    CGSize imageSize = self.imageView.frame.size;//imageSize	CGSize	(width = 18.5, height = 18.5)
    CGSize titleSize = self.titleLabel.frame.size;//textSize	CGSize	(width = 37, height = 22)
    CGSize textSize = [self.titleLabel.text sizeWithFont:self.titleLabel.font];//textSize	CGSize	(width = 37, height = 22)
    CGSize frameSize = CGSizeMake(ceilf(textSize.width), ceilf(textSize.height));//frameSize	CGSize	(width = 37, height = 22)
    
    if (titleSize.width + 0.5 < frameSize.width) {
        titleSize.width = frameSize.width;
    }
    CGFloat totalHeight = (imageSize.height + titleSize.height +_spacing);
    CGFloat totalWidth = (imageSize.width + titleSize.width +5);
    if (type==Top) {
        self.imageEdgeInsets = UIEdgeInsetsMake(- (totalHeight - imageSize.height),0.0,0.0,- titleSize.width);
        self.titleEdgeInsets = UIEdgeInsetsMake(0, - imageSize.width, - (totalHeight - titleSize.height), 0);
    }else if (type==Left){
        
        self.imageEdgeInsets = UIEdgeInsetsMake(0,-_spacing,0, 0);
        self.titleEdgeInsets = UIEdgeInsetsMake(0,0,0,-_spacing);
    }else if (type==Right){
        
        self.imageEdgeInsets = UIEdgeInsetsMake(0,_spacing,0,-titleSize.width*2);
        self.titleEdgeInsets = UIEdgeInsetsMake(0,-imageSize.width*2,0,_spacing);
    }else if (type==Bottom){
        
        self.imageEdgeInsets = UIEdgeInsetsMake(0.0,0.0,- (totalHeight - imageSize.height),- titleSize.width);
        self.titleEdgeInsets = UIEdgeInsetsMake( - (totalHeight - titleSize.height), - imageSize.width,0.0, 0);
    }
}

@end
